package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.enums.app.EnableEnum;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.Gift;
import com.yunkeji.dao.mapper.GiftMapper;
import com.yunkeji.dao.vo.GiftVo;
import com.yunkeji.system.domain.bo.GiftBo;
import com.yunkeji.system.service.IGiftService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 礼物配置信息Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class GiftServiceImpl implements IGiftService {

    private final GiftMapper baseMapper;

    /** 查询礼物配置信息 */
    @Override
    public GiftVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询礼物配置信息列表 */
    @Override
    public TableDataInfo<GiftVo> queryPageList(GiftBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Gift> lqw = buildQueryWrapper(bo);
        Page<GiftVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询礼物配置信息列表 */
    @Override
    public List<GiftVo> queryList(GiftBo bo) {
        LambdaQueryWrapper<Gift> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Gift> buildQueryWrapper(GiftBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Gift> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, Gift::getType, bo.getType());
        lqw.eq(bo.getSortId() != null, Gift::getSortId, bo.getSortId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Gift::getName, bo.getName());
        lqw.eq(bo.getPrice() != null, Gift::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getImgUri()), Gift::getImgUri, bo.getImgUri());
        lqw.eq(StringUtils.isNotBlank(bo.getSvgaUri()), Gift::getSvgaUri, bo.getSvgaUri());
        lqw.orderByAsc(Gift::getSortId);
        return lqw;
    }

    /** 新增礼物配置信息 */
    @Override
    public Boolean insertByBo(GiftBo bo) {
        Gift add = BeanUtil.toBean(bo, Gift.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改礼物配置信息 */
    @Override
    public Boolean updateByBo(GiftBo bo) {
        Gift update = BeanUtil.toBean(bo, Gift.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(Gift entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除礼物配置信息 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void del(Long id) {
        Gift gift = new Gift();
        gift.setId(id);
        gift.setDelFlag(EnableEnum.YES.getCode());
        baseMapper.updateById(gift);
    }

    @Override
    public void disable(Long id) {
        Gift gift = baseMapper.selectById(id);
        gift.setValidFlag(
                gift.getValidFlag().equals(EnableEnum.YES.getCode())
                        ? EnableEnum.NO.getCode()
                        : EnableEnum.YES.getCode());
        baseMapper.updateById(gift);
    }
}
